using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Reminder
{
    public partial class MainForm : Form
    {
        private RemindersDocument _doc;        
        private string _filename;
        private Timer _timer;

        public MainForm()
        {
            InitializeComponent();            
        }  

        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            foreach (Event evt in _doc.Events)
            {
                if (evt.LastRemind + evt.Interval < DateTime.Now)
                {
                    evt.LastRemind = DateTime.Now;
                    Alert(evt);
                }
            }
        }

        private void Save()
        {
            this._doc.Save();
        }

        private void Alert(Event evt)
        {
            string name = evt.Name;
            notifier.ShowBalloonTip(3000, Application.ProductName, name, ToolTipIcon.Info);
        }

        public void EventLists()
        {
            lstViewEvents.Items.Clear();
            foreach (Event evt in _doc.Events)
            {
                string[] item = new string[2];
                item[0] = evt.Name;
                item[1] = evt.IntervalString;
                ListViewItem list = new ListViewItem(item);
                list.Tag = evt;
                lstViewEvents.Items.Add(list);
            }
        }

        private void EditEvents()
        {
            if (lstViewEvents.SelectedItems.Count > 0)
            {
                ListViewItem item = lstViewEvents.SelectedItems[0];
                Event evt = (Event)item.Tag;
                NewEvent form = new NewEvent(_doc);
                form.Text = "Edit Event";
                form.Event = evt;
                if (form.ShowDialog() == DialogResult.OK)
                {
                    item.SubItems[0].Text = evt.Name;
                    item.SubItems[1].Text = evt.IntervalString;
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            lstViewEvents.View = View.Details;
            ColumnHeader columnHeader = new ColumnHeader();
            columnHeader.Width = lstViewEvents.Size.Width;

            _filename = Application.UserAppDataPath + "\\reminders.xml";
            _doc = File.Exists(_filename) ? RemindersDocument.Open(_filename) : new RemindersDocument(_filename);
            EventLists();

            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Start();
        }

        private void MainForm_Move(object sender, EventArgs e)
        {
            if (this == null)
            {
                return;
            }
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
            else
            {
                this.Show();
            }
        }

        private void notifier_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
            }
            this.Activate();
            this.Focus();
        }

        private void notifier_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            NewEvent form = new NewEvent(_doc);
            if (form.ShowDialog() == DialogResult.OK)
            {
                EventLists();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditEvents();            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            EventLists();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection events = lstViewEvents.SelectedItems;
            foreach (ListViewItem evnt in events)
            {
                Event evt = (Event)evnt.Tag;
                this._doc.Events.Remove(evt);
                Save();
                lstViewEvents.Items.Clear();
                EventLists();
            }
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void mnuShowWindow_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            notifier.Visible = false;
            Application.Exit();
        }

        private void lstViewEvents_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            EditEvents();
        }                
    }
}